/********************************************************************************
* Copyright (c) 2009, Matteo Pelati (matteo.pelati@gmail.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the <organization> nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Matteo Pelati ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**************************************************************************************/

#include "stdafx.h"
#include <iostream>
#include <exception>


#include "DbEnvironment.h"
#include "DbXmlManager.h"
#include "DbXmlQueryContext.h"
#include "DbException.h"
#include "DbXmlException.h"
#include "DbXmlTransaction.h"
#include "DbXmlResults.h"
#include "DbXmlUpdateContext.h"


namespace BerkeleyDB { namespace Xml
{


	DbXmlDocument^ DbXmlManager::CreateDocument()
	{
		try
		{
			return gcnew DbXmlDocument(this, n_dbEnv->createDocument());
		}
		rethrowDbXmlException();
	
	}

	DbXmlContainer^ DbXmlManager::CreateContainer(String^ name)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			return gcnew DbXmlContainer(this, n_dbEnv->createContainer(cname));
		}
		rethrowDbXmlException();
	}
	
	
	DbXmlContainer^ DbXmlManager::CreateContainer(String^ name, DbXmlTransaction^ tx)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			return gcnew DbXmlContainer(this, n_dbEnv->createContainer(*(tx->GetInnerObject()), cname));
		}
		rethrowDbXmlException();
	}
	
	
	DbXmlContainer^ DbXmlManager::CreateContainer(String^ name, DbXmlContainerInitFlags flags, DbXmlContainerType type)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			return gcnew DbXmlContainer(this, n_dbEnv->createContainer(
																cname, 
																(u_int32_t) flags, 
																(XmlContainer::ContainerType) type));
		}
		rethrowDbXmlException();
	}
	
	
	DbXmlContainer^ DbXmlManager::CreateContainer(String^ name, DbXmlTransaction^ tx, DbXmlContainerInitFlags flags, DbXmlContainerType type)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			return gcnew DbXmlContainer(this, n_dbEnv->createContainer(
																*(tx->GetInnerObject()),
																cname, 
																(u_int32_t) flags, 
																(XmlContainer::ContainerType) type));
		}
		rethrowDbXmlException();
	}

	DbXmlContainer^ DbXmlManager::OpenContainer(String^ name)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			return gcnew DbXmlContainer(this, n_dbEnv->openContainer(cname));
		}
		rethrowDbXmlException();
	}

	DbXmlContainer^ DbXmlManager::OpenContainer(String^ name, DbXmlTransaction^ txn)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			return gcnew DbXmlContainer(this, n_dbEnv->openContainer(
																*(txn->GetInnerObject()),
																cname
									));
		}
		rethrowDbXmlException();
	}

	DbXmlContainer^ DbXmlManager::OpenContainer(String^ name, DbXmlContainerInitFlags flags)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			return gcnew DbXmlContainer(this, n_dbEnv->openContainer(
																cname, 
																(u_int32_t) flags
									));
		}
		rethrowDbXmlException();
	}

	DbXmlContainer^ DbXmlManager::OpenContainer(String^ name, DbXmlTransaction^ txn, DbXmlContainerInitFlags flags)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			return gcnew DbXmlContainer(this, n_dbEnv->openContainer(
																*(txn->GetInnerObject()),
																cname, 
																(u_int32_t) flags
									));
		}
		rethrowDbXmlException();
	}

	DbXmlContainer^ DbXmlManager::OpenContainer(String^ name, DbXmlContainerInitFlags flags, DbXmlContainerType type)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			return gcnew DbXmlContainer(this, n_dbEnv->openContainer(
																cname, 
																(u_int32_t) flags,
																(XmlContainer::ContainerType) type
									));
		}
		rethrowDbXmlException();
	}

	DbXmlContainer^ DbXmlManager::OpenContainer(String^ name, DbXmlTransaction^ txn, DbXmlContainerInitFlags flags, DbXmlContainerType type)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			return gcnew DbXmlContainer(this, n_dbEnv->openContainer(
																*(txn->GetInnerObject()),
																cname, 
																(u_int32_t) flags,
																(XmlContainer::ContainerType) type
									));
		}
		rethrowDbXmlException();
	}

	void DbXmlManager::ReindexContainer(String^ name, DbXmlUpdateContext^ ctx, DbXmlContainerInitFlags flags)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			n_dbEnv->reindexContainer(cname, *(ctx->GetInnerObject()), (u_int32_t) flags);
			
		}
		rethrowDbXmlException();
	}

	void DbXmlManager::ReindexContainer(DbXmlTransaction^ txn, String^ name, DbXmlUpdateContext^ ctx, DbXmlContainerInitFlags flags)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			n_dbEnv->reindexContainer(*(txn->GetInnerObject()), cname, *(ctx->GetInnerObject()), (u_int32_t) flags);
			
		}
		rethrowDbXmlException();
	}

	void DbXmlManager::RemoveContainer(DbXmlTransaction^ txn, String^ name)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			n_dbEnv->removeContainer(*(txn->GetInnerObject()), cname);
		}
		rethrowDbXmlException();
	}
	
	void DbXmlManager::RemoveContainer(String^ name)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			n_dbEnv->removeContainer(cname);
		}
		rethrowDbXmlException();
	}

	void DbXmlManager::RenameContainer(String^ oldname, String^ newname)
	{
		try
		{
			CLRSTR2STDSTR(c_oldname, oldname);
			CLRSTR2STDSTR(c_newname, newname);
			n_dbEnv->renameContainer(c_oldname, c_newname);
		}
		rethrowDbXmlException();
	}
	void DbXmlManager::RenameContainer(DbXmlTransaction^ txn, String^ oldname, String^ newname)
	{
		try
		{
			CLRSTR2STDSTR(c_oldname, oldname);
			CLRSTR2STDSTR(c_newname, newname);
			n_dbEnv->renameContainer(*(txn->GetInnerObject()), c_oldname, c_newname);
		}
		rethrowDbXmlException();
	}


	DbXmlTransaction^ DbXmlManager::CreateTransaction(DbXmlTxnCreateFlags flags)
	{
		try
		{
			return gcnew DbXmlTransaction(this, n_dbEnv->createTransaction((u_int32_t) flags), flags);
		}
		rethrowDbXmlException();
	}

	int DbXmlManager::ExistsContainer(String^ name)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			return n_dbEnv->existsContainer(cname);
		}
		rethrowDbXmlException();
	}

	DbXmlUpdateContext^ DbXmlManager::CreateUpdateContext()
	{
		try
		{
			return gcnew DbXmlUpdateContext(this, n_dbEnv->createUpdateContext());
		}
		rethrowDbXmlException();
	}

	DbXmlModify^ DbXmlManager::CreateModify()
	{
		try
		{
			return gcnew DbXmlModify(this, n_dbEnv->createModify());
		}
		rethrowDbXmlException();
	}

	DbXmlQueryContext^ DbXmlManager::CreateQueryContext(DbXmlQueryReturnType rtype, DbXmlQueryEvalType evtype)
	{
		try
		{
			return gcnew DbXmlQueryContext(this, n_dbEnv->createQueryContext((XmlQueryContext::ReturnType)rtype, (XmlQueryContext::EvaluationType) evtype ));
		}
		rethrowDbXmlException();
	}

	DbXmlResults^ DbXmlManager::CreateResults()
	{
		try
		{
			return gcnew DbXmlResults(this, n_dbEnv->createResults(), nullptr, nullptr);
		}
		rethrowDbXmlException();
	}

	void DbXmlManager::CompactContainer(String^ name, DbXmlUpdateContext^ uctx)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			n_dbEnv->compactContainer(cname, *(uctx->GetInnerObject()));
		}
		rethrowDbXmlException();
	}
	
	
	void DbXmlManager::CompactContainer(DbXmlTransaction^ txn, String^ name, DbXmlUpdateContext^ uctx)
	{
		try
		{
			CLRSTR2STDSTR(cname, name);
			n_dbEnv->compactContainer(*(txn->GetInnerObject()), cname, *(uctx->GetInnerObject()));
		}
		rethrowDbXmlException();
	}

	IEnumerable<DbXmlValue^>^ DbXmlManager::Query(String^ xquery, DbXmlQueryContext^ ctx, DbXmlQueryContextFlags flags)
	{
		try
		{
			CLRSTR2STDSTR(cxquery, xquery);
			return (IEnumerable<DbXmlValue^>^) gcnew DbXmlValueCollection(this, n_dbEnv->query(cxquery, *(ctx->GetInnerObject()), (u_int32_t) flags), nullptr, ctx);
		}
		rethrowDbXmlException();
	}
	
	
	IEnumerable<DbXmlValue^>^ DbXmlManager::Query(DbXmlTransaction^ txn, String^ xquery, DbXmlQueryContext^ ctx, DbXmlQueryContextFlags flags)
	{
		try
		{
			CLRSTR2STDSTR(cxquery, xquery);
			return (IEnumerable<DbXmlValue^>^) gcnew DbXmlValueCollection(this, n_dbEnv->query(*(txn->GetInnerObject()), cxquery, *(ctx->GetInnerObject()), (u_int32_t) flags), txn, ctx);
		}
		rethrowDbXmlException();
	}

	DbXmlQueryExpression^ DbXmlManager::Prepare(String^ xquery, DbXmlQueryContext^ ctx)
	{
		try
		{
			CLRSTR2STDSTR(cxquery, xquery);
			return gcnew DbXmlQueryExpression(this, n_dbEnv->prepare(cxquery, *(ctx->GetInnerObject())));
		}
		rethrowDbXmlException();
	}


	DbXmlQueryExpression^ DbXmlManager::Prepare(DbXmlTransaction^ txn, String^ xquery, DbXmlQueryContext^ ctx)
	{
		if (txn == nullptr) return Prepare(xquery, ctx);
		try
		{
			CLRSTR2STDSTR(cxquery, xquery);
			return gcnew DbXmlQueryExpression(this, n_dbEnv->prepare(*(txn->GetInnerObject()), cxquery, *(ctx->GetInnerObject())));		
		}
		rethrowDbXmlException();
	}


}}